const appConfig = require('../../config/config.js');
const http = require('../../utils/http.js');
const nav = require('../../utils/navigator.js');
const util = require('../../utils/util.js');
//获取应用实例
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    noticeType: 2,
    hiddenModal: true,
    pageIndex: {}, //当前页码
    pageData: {}, //分页数据
    loadMoreText: '正在载入更多...', //上拉加载提示文本
    hideBottom: true, //上拉加载提示层
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.onQuanziLoad();
  },
  bindChangeType: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.type);
    var type = e.currentTarget.dataset.type;
    that.setData({ noticeType: type, pageIndex: 1, pageData:[] });
    if(type==2){
      that.onQuanziLoad();
    }else{
      that.onNoticeLoad();
    }
  },
  //列表点击事件
  item_click: function (e) {
    //console.log(e.currentTarget.dataset.noticeId);
    nav.navigateTo('../blogger/blogger', {
      userId: e.currentTarget.dataset.userId
    });
  },
  //距离计算
  calculateDistance: function (data) {
    var that = this;
    var result = data;
    var distance = 0;
    for (var i = 0; i < data.length; i++) {
      distance = util.getDistance(app.globalData.locationInfo.latitude, app.globalData.locationInfo.longitude, data[i].Latitude, data[i].Longitude);
      result[i].distance = distance;
    }
    return result;
  },
  onQuanziLoad: function () {
    var that = this;
    wx.showLoading({
      title: '玩命加载中...',
    });
    //加载列表
    http.post(appConfig.baseUrl + 'Mine/QueryArticleList', {
      UserId: wx.getStorageSync('userId'),
      PageIndex: 1
    },
      function (ret) { //
        console.log(ret.data);
        wx.hideLoading(); //关闭加载动画
        that.setData({
          pageIndex: 1, //当前页码
          pageData: ret.data, //分页数据
          hideBottom: true,
          loadMoreText: '正在载入更多...'
        });

      });
   },
  onNoticeLoad: function () { 
    var that = this;
    wx.showLoading({
      title: '玩命加载中...',
    });
    //console.log(that.data.pageIndex);
    http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', {
      UserId: wx.getStorageSync('userId'),
      PageIndex: 1,
      NoticeType: that.data.noticeType
    },
      function (res) { //
        console.log(res.data);
        wx.hideLoading(); //关闭加载动画
        var result = that.calculateDistance(res.data);
        if (res.data.length > 0) {
          var data_list = that.data.pageData;
          for (var i = 0; i < res.data.length; i++) {
            data_list.push(res.data[i]);
          }
          that.setData({
            pageData: data_list, //分页数据
            hideBottom: true
          });
          //console.log(that.data.pageData);
        } else {
          //没有更多了
          that.setData({
            loadMoreText: "没有更多数据了", //分页数据
            hideBottom: false,
            pageIndex: that.data.pageIndex - 1
          });
        }
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    var ntype = that.data.noticeType;
    if (ntype == 2)
      that.onQuanziLoad();
      else
      that.onNoticeLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var ntype=that.data.noticeType;
    // 页数+1
    that.setData({
      pageIndex: that.data.pageIndex + 1,
      hideBottom: false
    });
    if(ntype==2)
    {
      wx.showLoading({
        title: '玩命加载中...',
      });
      http.post(appConfig.baseUrl + 'Mine/QueryArticleList', {
        UserId: wx.getStorageSync('userId'),
        PageIndex: that.data.pageIndex,
      },
        function (res) { //
          wx.hideLoading(); //关闭加载动画
          //console.log(res.data);
          if (res.data.length > 0) {
            that.setData({
              pageData: res.data, //分页数据
              hideBottom: true,
              loadMoreText: '正在载入更多...'
            });
            //console.log(that.data.pageData);
          } else {
            //没有更多了
            that.setData({
              loadMoreText: "没有更多数据了", //分页数据
              hideBottom: false,
              pageIndex: that.data.pageIndex - 1
            });
          }
        });
    }
    else
    {
      wx.showLoading({
        title: '玩命加载中...',
      });
      http.post(appConfig.baseUrl + 'Notice/QueryNoticeList', {
        UserId: wx.getStorageSync('userId'),
        PageIndex: that.data.pageIndex,
        NoticeType: that.data.noticeType
      },
        function (res) { //
          wx.hideLoading(); //关闭加载动画
          //console.log(res.data);
          var result = that.calculateDistance(res.data);
          if (res.data.length > 0) {
            var data_list = that.data.pageData;
            for (var i = 0; i < res.data.length; i++) {
              data_list.push(res.data[i]);
            }
            that.setData({
              pageData: data_list, //分页数据
              hideBottom: true,
              loadMoreText: '正在载入更多...'
            });
            //console.log(that.data.pageData);
          } else {
            //没有更多了
            that.setData({
              loadMoreText: "没有更多数据了", //分页数据
              hideBottom: false,
              pageIndex: that.data.pageIndex - 1
            });
          }
        });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})